﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class Convertation
    {
        /// <summary>
        /// Converts a string to a double array.
        /// Numbers in the string should be splitted by space symbols.
        /// </summary>
        /// <param name="input">String to convert.</param>
        /// <returns>Double array.</returns>
        public static double[] StringToDoubleArray(string input)
        {
            string[] substrings = input.Split(' ');
            List<double> result = new List<double>();

            foreach (var str in substrings)
            {
                if (!String.IsNullOrEmpty(str))
                {
                    result.Add(Convert.ToDouble(str, System.Globalization.CultureInfo.InvariantCulture));
                }
            }

            return result.ToArray();
        }

        public static String DoubleArrayToString(Double[] input)
        {
            String result = String.Empty;

            foreach (Double d in input)
            {
                result += String.Format("{0} ", d.ToString().Replace(',', '.'));
            }

            result = result.Substring(0, result.Length - 1);

            return result;
        }
    }
}
